package com.example.japinhands.ui.screen

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.example.japinhands.JapViewModel
import com.example.japinhands.data.AdjData
import com.example.japinhands.data.DeformAdj

@Composable
fun DeformAdjScreen(
    viewModel: JapViewModel,
    update: Boolean,
    adj: DeformAdj? = null
) {
    val tag = "NewDeformAdjScreen"
    var adjData by remember {
        mutableStateOf(AdjData())
    }
    var deformAdj1 by remember {
        mutableStateOf("")
    }
    var deformAdj2 by remember {
        mutableStateOf("")
    }
    var deformAdj3 by remember {
        mutableStateOf("")
    }
    var deformAdj4 by remember {
        mutableStateOf("")
    }
    val saveNewDeform: () -> Unit = {
        val deform = DeformAdj(
            deform1 = deformAdj1,
            deform2 = deformAdj2,
            deform3 = deformAdj3,
            deform4 = deformAdj4,
        )
        viewModel.saveDeformAdj(deform) {
            viewModel.allowSubmit = true
        }
    }

    val handler by remember {
        mutableStateOf(object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                if (msg.what == 123) saveNewDeform.invoke()
            }
        })
    }

    val afterChange: () -> Unit = {
        viewModel.allowSubmit = false
        handler.apply {
            removeCallbacksAndMessages(null)
            sendEmptyMessageDelayed(123, 1000)
        }
    }

    LaunchedEffect(adj) {
        adjData = getUIData(update, adj, viewModel)
        deformAdj1 = adjData.d1
        deformAdj2 = adjData.d2
        deformAdj3 = adjData.d3
        deformAdj4 = adjData.d4
        saveNewDeform.invoke()
    }

    Column {
        LeftRightItem(
            leftLabel = "原型",
            rightLabel = "否定",
            leftText = deformAdj1,
            rightText = deformAdj2,
            onLeftChange = { deformAdj1 = it; afterChange.invoke() },
            onRightChange = { deformAdj2 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "过去式",
            rightLabel = "过去式否定",
            leftText = deformAdj3,
            rightText = deformAdj4,
            onLeftChange = { deformAdj3 = it; afterChange.invoke() },
            onRightChange = { deformAdj4 = it; afterChange.invoke() }
        )
//        Button(
//            modifier = Modifier.fillMaxWidth(),
//            onClick = {
//                thread {
//                    val id = viewModel.insertDeformAdj(
//                        DeformAdj(
//                            deform1 = deformVerb1,
//                            deform2 = deformVerb2,
//                            deform3 = deformVerb3,
//                            deform4 = deformVerb4
//                        )
//                    )
//                    Log.d(tag, "Insert deform adj, id: $id")
//                }
//            }
//        ) {
//            Text(text = "Submit")
//        }
    }
}

private fun getUIData(update: Boolean, adj: DeformAdj? , viewModel: JapViewModel) = if (update) {
    var res = AdjData()
    adj?.let {
        res = AdjData(it)
    }
    res
} else {
    AdjData(
        d1 = viewModel.deformAdjLiveData.value?.deform1 ?: "",
        d2 = viewModel.deformAdjLiveData.value?.deform2 ?: "",
        d3 = viewModel.deformAdjLiveData.value?.deform3 ?: "",
        d4 = viewModel.deformAdjLiveData.value?.deform4 ?: ""
    )
}